package com.campus.customer.controller;

import com.campus.common.bean.common.Resp;
import com.campus.customer.bean.user.UserBean;
import com.campus.customer.entity.User;
import com.campus.customer.service.SessionService;
import com.campus.customer.service.UserService;
import com.campus.customer.vo.UserInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-04-02
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    
    @Autowired
    private SessionService sessionService;

    /**
     * 获取当前用户信息
     * @return 用户信息
     */
    @GetMapping("/info")
    public Resp<UserInfoVo> getUserInfo() {
        UserInfoVo userInfo = sessionService.getUserInfo();
        User user = userService.getById(userInfo.getId());

        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(user, userInfoVo);
        return Resp.success(userInfoVo);
    }

    /**
     * 更新用户信息
     * @param id 用户ID
     * @param userBean 用户信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    public Resp<Void> updateUser(@PathVariable Integer id, @RequestBody UserBean userBean) {
        userService.updateUserInfo(id, userBean);
        return Resp.success();
    }

}
